/*
 * Copyright (c) 2010-2011 Josef Hardi <josef.hardi@gmail.com>
 * Copyright (c) 2010 Piotr Zagawa <piotr.zagawa@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package id.duapuluhdua.dakidaki;

import java.util.Set;

import android.os.Bundle;

/**
 *
 */
public class Request
{
  /** Constants */
  private final static String KEY_ROWID = "KEY_ROWID";
  private final static String KEY_MODE = "KEY_MODE";

  public final static int MODE_NONE = 0;
  public final static int MODE_VIEW = 1;
  public final static int MODE_EDIT = 2;
  public final static int MODE_INSERT = 3;

  /** Fields */
  private Bundle mData;

  /**
   * Creates a command data with assigned mode.
   *
   * @param mode
   */
  public Request(int mode)
  {
    mData = new Bundle();
    mData.putInt(KEY_MODE, mode);
    mData.putLong(KEY_ROWID, -1);
  }

  /**
   * Creates a command data with assigned mode and row id.
   *
   * @param mode
   * @param lRowID
   */
  public Request(int mode, long rowId)
  {
    mData = new Bundle();
    mData.putInt(KEY_MODE, mode);
    mData.putLong(KEY_ROWID, rowId);
  }

  /**
   * Creates a command data with assigned bundle data.
   *
   * @param data
   */
  public Request(Bundle data)
  {
    if (data == null) {
      mData = new Bundle();
    }
    else {
      mData = data;
    }
  }

  /**
   * Sets an edit mode to a command container.
   *
   * @param container
   * @return
   */
  public static Request setModeEdit(Request container)
  {
    if (container == null) {
      return new Request(MODE_EDIT);
    }
    else {
      container.mData.putInt(KEY_MODE, MODE_EDIT);
      return container;
    }
  }

  /**
   * Sets an insert mode to a command container.
   *
   * @param container
   * @return
   */
  public static Request setModeInsert(Request container)
  {
    if (container == null) {
      return new Request(MODE_INSERT);
    }
    else {
      container.mData.putInt(KEY_MODE, MODE_INSERT);
      return container;
    }
  }

  /**
   * Sets a view mode to a command container.
   *
   * @param container
   * @return
   */
  public static Request setModeView(Request container)
  {
    if (container == null) {
      return new Request(MODE_VIEW);
    }
    else {
      container.mData.putInt(KEY_MODE, MODE_VIEW);
      return container;
    }
  }

  /**
   * Retrieves the bundle data.
   *
   * @return
   */
  public Bundle getBundle()
  {
    return mData;
  }

  /**
   *
   * @param rowId
   */
  public void setRowId(long rowId)
  {
    mData.putLong(KEY_ROWID, rowId);
  }

  /**
   *
   * @return
   */
  public long getRowId()
  {
    if (mData.containsKey(KEY_ROWID)) {
      return mData.getLong(KEY_ROWID);
    }
    else {
      return -1;
    }
  }

  /**
   * Updates the command to edit mode.
   */
  public void setModeEdit()
  {
    mData.putInt(KEY_MODE, MODE_EDIT);
  }

  /**
   * Updates the command to insert mode.
   */
  public void setModeInsert()
  {
    mData.putInt(KEY_MODE, MODE_INSERT);
  }

  /**
   * Updates the command to view mode.
   */
  public void setModeView()
  {
    mData.putInt(KEY_MODE, MODE_VIEW);
  }

  public boolean isViewMode()
  {
    if (mData.containsKey(KEY_MODE))
      return (mData.getInt(KEY_MODE) == MODE_VIEW);
    return false;
  }

  public boolean isEditMode()
  {
    if (mData.containsKey(KEY_MODE))
      return (mData.getInt(KEY_MODE) == MODE_EDIT);
    return false;
  }

  public boolean isInsertMode()
  {
    if (mData.containsKey(KEY_MODE))
      return (mData.getInt(KEY_MODE) == MODE_INSERT);
    return false;
  }

  @Override
  public String toString()
  {
    final Set<String> keys = mData.keySet();

    String items = "";
    for (String key : keys) {
      items += key + ": " + mData.get(key) + "\n";
    }

    return items;
  }

  public void setValue(String key, int value)
  {
    mData.putInt(key, value);
  }

  public void setValue(String key, boolean value)
  {
    mData.putBoolean(key, value);
  }

  public int getValueInt(String key)
  {
    if (mData.containsKey(key))
      return mData.getInt(key);
    else
      return -1;
  }
}
